package com.neu;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.neu.activity.LoginActivity;
import com.neu.bean.Rotation;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    //1,定义一个按钮对象B
    Button btnBack;
    TextView tvIP;
    private ImageView myImg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //2.创建一个initView方法
        initView();
        //读取IP写入到txtview中
        getSp();
        //加载图片
        loadImage();

    }

    private void loadImage() {
        //新建一个volley队列
        RequestQueue queue= Volley.newRequestQueue(this);
        //请求接口的地址
        String method="http://124.93.196.45:10001/prod-api/api/rotation/list?pageNum=1&pageSize=8&type=2";
        JsonObjectRequest jr=new JsonObjectRequest(Request.Method.GET, method, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                String json=jsonObject.toString();
                Log.d("tag", "onResponse: "+json);
                tvIP.setText(json);
                Gson gson=new Gson();
                Rotation rotation=gson.fromJson(json,Rotation.class);

                String url="http://124.93.196.45:10001"+rotation.getRows().get(0).getAdvImg();

                Glide.with(MainActivity.this).load(url).into(myImg);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.fillInStackTrace();
            }
        });

        //添加到队列中
        queue.add(jr);


    }

    private void getSp() {
        //1.
        SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        String ip = sp.getString("ip", "127.0.0.1");
        tvIP.setText(ip);
    }

    private void initView() {
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });

        tvIP = findViewById(R.id.tvIP);


        myImg = (ImageView) findViewById(R.id.myImg);

    }

    //3.在initView方法中获得对象的引用
    //4.在initView方法中定义按钮事件
    //5.在按钮事件中创建Intent对象

}